﻿// JScript File
var f = null;

function getControl(id) {
    var control = id;
    if(typeof control == 'string') {
        control = document.getElementById(id);
    }
    return control;
}

function setEnabled(id, enable)
{
    var control = getControl(id);
    if(!control) return;
    if(enable) control.disabled = '';
    else control.disabled = 'disabled';
} 

function setVisible(id, visible)
{
    var control = getControl(id);
    if(!control) return;
    if(visible == false)
    {
        var parent = control.parentNode;
        parent.removeChild(control);
    }   
}    

function getVisible(id)
{
    var control = getControl(id);
    if(!control) return false;
    return true;
}  

function setEnabledAfterTimeOut(id, enable, time)
{
    f = "setEnabled('" + id + "', " + enable + ");";
    setTimeout("eval(f)", time)
}       

var choiceBehavior = {
    checkboxIdTemplate : 'selectedCheckBox',
    checkedItems : 0,
    totalItems : 0,
    btnActions : null,
    checkAll : null,
    
    doCheckAll : function(checked) {
        var i;
        var checkbox;
        for (i = 0; i < choiceBehavior.totalItems; i ++) {
            checkbox = document.getElementById(choiceBehavior.checkboxIdTemplate + i);
            if (checkbox) { checkbox.checked = checked; }                
        }
        if(checked){ 
            choiceBehavior.checkedItems = choiceBehavior.totalItems;
            if (choiceBehavior.totalItems > 0) { choiceBehavior.changeStatusActionButtons(true); }
        } else { 
            choiceBehavior.checkedItems = 0;         
            choiceBehavior.changeStatusActionButtons(false);
        }    
    },
    
    checkChanged : function (checked) { 
        if (!checked) {
            choiceBehavior.checkedItems--;
            choiceBehavior.checkAll.checked = false;       
        } else {
            choiceBehavior.checkedItems++;
            choiceBehavior.checkAll.checked = choiceBehavior.totalItems == choiceBehavior.checkedItems;
        }
        if (choiceBehavior.checkedItems == 0) { choiceBehavior.changeStatusActionButtons(false); }            
        else { choiceBehavior.changeStatusActionButtons(true); }
    },
    
    changeStatusActionButtons : function (value) {
        if (choiceBehavior.btnActions.constructor == Array) {
            for (var i = 0; i < choiceBehavior.btnActions.length; i++)
            {
                setEnabled(choiceBehavior.btnActions[i], value);
            }
        } else { setEnabled(choiceBehavior.btnActions, value); }
    },
    
    init : function () {
        var itemChecked = 0;
        for (i = 0; i < choiceBehavior.totalItems; i ++) {
            checkbox = document.getElementById(choiceBehavior.checkboxIdTemplate + i);
            if (checkbox.checked) { itemChecked++; break;}                
        }
        if(itemChecked == 0)    
            choiceBehavior.changeStatusActionButtons(false);
        else
            choiceBehavior.changeStatusActionButtons(true);
    }
}

function openWindow(url, name, width, height) {
   var atts = 'width=' + width + ',height=' + height + ',toolbar=no,scrollbars=yes,location=no,menubar=no';
    window.open(url,name,atts);
}

